package com.weiyi.portal.modules.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weiyi.portal.modules.ums.model.UmsFriendsRelation;
import com.weiyi.portal.modules.ums.mapper.UmsFriendsRelationMapper;
import com.weiyi.portal.modules.ums.model.UmsMember;
import com.weiyi.portal.modules.ums.service.UmsFriendsRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weiyi.portal.modules.ums.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author PZC
 * @since 2022-11-04
 */
@Service
public class UmsFriendsRelationServiceImpl extends ServiceImpl<UmsFriendsRelationMapper, UmsFriendsRelation> implements UmsFriendsRelationService {
    @Autowired
    UmsFriendsRelationMapper umsFriendsRelationMapper;
    @Autowired
    UmsMemberService umsMemberService;

    @Override
    public Boolean isFollowed(String fromName, String toName) {
        QueryWrapper<UmsFriendsRelation> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(UmsFriendsRelation::getFromUser, fromName).eq(UmsFriendsRelation::getToUser, toName);

        UmsFriendsRelation friendsRelation = umsFriendsRelationMapper.selectOne(queryWrapper);
        if(friendsRelation==null)return false;
        return true;
    }

    @Override
    public List<String> getFriends(Integer id) {
        UmsMember member = umsMemberService.getById(id);

        QueryWrapper<UmsFriendsRelation>queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(UmsFriendsRelation::getFromUser, member.getUsername());
        List<UmsFriendsRelation> friendsRelations = umsFriendsRelationMapper.selectList(queryWrapper);
        List<String> friendsName=new ArrayList<>();
        for(UmsFriendsRelation relation:friendsRelations){
            friendsName.add(relation.getToUser());
        }
        return friendsName;
    }

    @Override
    public Boolean follow(String fromName, String toName) {
        UmsFriendsRelation relation = new UmsFriendsRelation();
        relation.setFromUser(fromName);
        relation.setToUser(toName);
        relation.setStatus(1);
        relation.setCreateTime(new Date());

        QueryWrapper<UmsFriendsRelation>queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(UmsFriendsRelation::getFromUser, fromName).eq(UmsFriendsRelation::getToUser, toName);
        UmsFriendsRelation selectOne = umsFriendsRelationMapper.selectOne(queryWrapper);
        if(selectOne!=null)return false;
        else{
            return umsFriendsRelationMapper.insert(relation) == 1;
        }

    }

    @Override
    public List<UmsMember> getFans(String userName) {
        return null;
    }

    @Override
    public Boolean unFollow(String fromName, String toName) {
        QueryWrapper<UmsFriendsRelation>queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(UmsFriendsRelation::getFromUser, fromName).eq(UmsFriendsRelation::getToUser, toName);
        UmsFriendsRelation selectOne = umsFriendsRelationMapper.selectOne(queryWrapper);

        if(selectOne==null)return false;
        else{
            return umsFriendsRelationMapper.delete(queryWrapper) == 1;
        }

    }
}
